package arr

/*
时间复杂度:O(N^2)
空间复杂度:O(1)

暴力破解

参考:https://leetcode-cn.com/problems/sum-of-subarray-minimums/
*/
func subArrayRanges(nums []int) int64 {
	var result int64
	for i := 0; i < len(nums); i++ {
		maxVal, minVal := nums[i], nums[i]
		for j := i + 1; j < len(nums); j++ {
			if nums[j] > maxVal {
				maxVal = nums[j]
			}
			if nums[j] < minVal {
				minVal = nums[j]
			}
			result += int64(maxVal - minVal)
		}
	}
	return result
}
